/*
 * FileDescriptor.cpp
 *
 *  Created on: 26/06/2014
 *      Author: Vitor
 */

#include "FileDescriptor.h"

FileDescriptor::FileDescriptor(std::string name, unsigned int location,
		unsigned int copy_location, unsigned int size) :
		FileSystemElement(name), location(location), copy_location(
				copy_location), size(size) {
}

FileDescriptor::~FileDescriptor() {
}

std::string FileDescriptor::html_encoding() {
	char ipAddr[20];
	unsigned int bit24 = 0xFF000000;
	unsigned int bit16 = 0x00FF0000;
	unsigned int bit8 = 0x0000FF00;
	unsigned int bit0 = 0x000000FF;

	sprintf(ipAddr, "%u.%u.%u.%u", (location & bit0), (location & bit8) >> 8,
			(location & bit16) >> 16, (location & bit24) >> 24);
	std::string ip(ipAddr);
	std::string href("http://" + ip + ":8080/" + name);
	auto file_name = name.substr(name.find_last_of("/") + 1);

	std::string html(
			"\t\t\t<tr>\n\t\t\t\t<td>\n\t\t\t\t\t<a href=\"" + href
					+ "\" download=\"" + file_name
					+ "\"><font color=\"white\" size=\"5\">" + file_name
					+ "</font></a>\n\t\t\t\t</td>\n\t\t\t\t<td>\n\t\t\t\t\t<form action=\"\" method=\"POST\" name=\""
					+ file_name
					+ "\">\n\t\t\t\t\t\t<input type=\"hidden\" name=\"form\" value=\""
					+ file_name
					+ "\">\n\t\t\t\t\t\t<select name=\"acao\">\n\t\t\t\t\t\t\t<option value=\"Renomear\">Renomear</option>\n\t\t\t\t\t\t\t<option value=\"Deletar\">Deletar</option>\n\t\t\t\t\t\t</select>\n\t\t\t\t\t\t<input type=\"text\" name=\"newName\">\n\t\t\t\t\t\t<input type=\"submit\" value=\"OK\">\n\t\t\t\t\t</form>\n\t\t\t\t</td>\n\t\t\t</tr>\n");
	//TODO: terminar escrita
	return html;
}

ByteQueue FileDescriptor::serialize() {
	ByteQueue bytes;
	bytes.push<char>(FILE_DESCRIPTOR);
	bytes.push<std::string>(name);
	bytes.push<unsigned int>(location);
	bytes.push<unsigned int>(copy_location);
	bytes.push<unsigned int>(size);
	return bytes;
}
